package org.gpswalker.sys.abstractions;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.gpswalker.sys.components.TemplateConfigurationSingleton;
import org.gpswalker.sys.server.users.CmsUser;

import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

/* XXX It is quite possible by the way, that we should extract the printing
 * functionality into the base abstract servlet. Thus we could ensure that 
 * the headers have been printed properly. 
 */

/**
 * This is the servlet layer that should take care of working with the 
 * template engine. And by the way, we could sometimes just print the contents 
 * of our datamodel without actually transforming it into html by a template.
 * To all probability, we can use a special template for that. ;)
 * 
 * @author dtv
 * 
 */
public abstract class AbstractHeaderWritingServlet extends AbstractSioServlet {

    private static final long serialVersionUID = -3946529976719057746L;

    protected Map<String, Object> dataModel;
    protected ResourceBundle      strings;
    
    private String title = "In order to set title, use setTitle(String s)";

    @Override
    public void service(HttpServletRequest req, HttpServletResponse res) {
	super.makeSio(req,res);
	initTemplating();
	arbeiten();
	processTemplate();
    }
    

    protected void initTemplating() {
        strings = ResourceBundle.getBundle("org.gpswalker.sys.i18n.ArticleStrings",
                sio.getRequest().getLocale());
        //FIXME in the line above we should really somehow get the package of this thing.

        dataModel = new HashMap<String, Object>();
        List<String> l = new ArrayList<String>();
        l.add("/Cms.css");
        dataModel.put("stylesheets", l);
        printLoginHeaders();
    }
    
    protected void processTemplate() {
	dataModel.put("title", title);
        TemplateConfigurationSingleton.getInstance().processTemplate(
                templateName(), sio.getResponse(), dataModel);
    }

    abstract protected String templateName();
    abstract protected void arbeiten();
    
    protected void setTitle(String t) {
	if (t != null) {
	    title = t;
	}
    }
    
    
    
    //FIXME User login functionality... Should it really be here?
    private void printLoginHeaders() {
	UserService us = UserServiceFactory.getUserService();

	if (CmsUser.user().isAnonymous()) {

	    dataModel.put("loginWord", strings.getString("login"));
	    dataModel.put("loginLink", us.createLoginURL(sio.getRequest()
		    .getRequestURI()));

	} else {
	    if (CmsUser.user().isOurs()) {
		    dataModel.put("loginWord", "You are registered with this site!");
		} else {
		    dataModel.put("notOurs", true);
		}
	    dataModel.put("loginWord", strings
		    .getString("hello user, first part.")
		    + CmsUser.user().getNickname()
		    + strings.getString("hello user, second part"));
	    dataModel.put("loginLink", us.createLogoutURL(sio.getRequest()
		    .getRequestURL().toString()));
	}
    }

}
